import Cookies from 'js-cookie'
import { getAppVersion } from '@/api/application'

const app = {
  namespaced: true,
  state: {
    // 菜单收起、展开
    sidebar: {
      opened: ~~(Cookies.get('sidebarStatus') as any),
      withoutAnimation: false
    },
    appUrl: '',
    device: 'desktop',
    // 图片上传到系统的链接，固定写死的
    // 例如 'http://api.istarscloud.com:8081/api/device/image/'
    actionUrl: 'http://139.155.92.140', // http://39.104.109.201:8081/file/api/upload/image
    // 组态前端url  显示接'viewer/'  编辑'app/editor/'  那边改了 这边要随时变
    scadaUrl: 'http://139.155.92.140:7081/', // 0.138:4200     http://fes.natsuki.cc:8028/  8022
    // theme: Cookies.get('themeColor') ? Cookies.get('themeColor') : '409EFF',
    theme: '252C47',
    // sys跳转标识
    sysSign: sessionStorage.getItem('sysSign')
      ? JSON.parse(sessionStorage.getItem('sysSign') || '')
      : '',
    // 用户可访问项目ID数组
    userProject: Cookies.get('userProject') ? Cookies.get('userProject') : null,
    logo: ''
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
      state.sidebar = { ...state.sidebar }
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', '1')
      } else {
        Cookies.set('sidebarStatus', '0')
      }
    },
    TOGGLE_THEME: (state, themeColor) => {
      Cookies.set('themeColor', themeColor)
      state.theme = themeColor
    },
    TOGGLE_SYS_SIGN: (state, sysSign) => {
      if (sysSign.returnSys) {
        sessionStorage.removeItem('sysSign')
        state.sysSign = null
      } else {
        sessionStorage.setItem('sysSign', JSON.stringify(sysSign))
        state.sysSign = sysSign
      }
    },
    TOGGLE_LOGO: (state, logo) => {
      state.logo = logo
    },
    SET_USER_PROJECT: (state, userProject) => {
      if (userProject.isSet) {
        Cookies.set('userProject', userProject.pData)
        state.userProject = userProject.pData
      } else {
        Cookies.remove('userProject')
        state.userProject = null
      }
      console.log(userProject, 'userProject')
    },
    REAL_TIME_MONITOR: (state, rtmQueryList) => {
      sessionStorage.setItem('rtmQueryList', JSON.stringify(rtmQueryList))
      state.rtmQueryList = rtmQueryList
    },
    HISTORY_TIME_MONITOR: (state, htmQueryList) => {
      sessionStorage.setItem('htmQueryList', JSON.stringify(htmQueryList))
      console.log('--------------set-----------', htmQueryList)
      state.htmQueryList = htmQueryList
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', '1')
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SET_APP_VERSION: (state, url) => {
      state.appUrl = url
      console.log(state.appUrl)
    }
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    ToggleTheme: ({ commit }, themeColor) => {
      commit('TOGGLE_THEME', themeColor)
    },
    ToggleSysSign: ({ commit }, sysSign) => {
      commit('TOGGLE_SYS_SIGN', sysSign)
    },
    ToggleLogo: ({ commit }, logo) => {
      commit('TOGGLE_LOGO', logo)
    },
    SetUserProject: ({ commit }, userProject) => {
      commit('SET_USER_PROJECT', userProject)
    },
    RealTimeMonitor: ({ commit }, rtmQueryList) => {
      commit('REAL_TIME_MONITOR', rtmQueryList)
    },
    HistoryTimeMonitor: ({ commit }, htmQueryList) => {
      commit('HISTORY_TIME_MONITOR', htmQueryList)
    },
    CloseSideBar: ({ commit }, { withoutAnimation }) => {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    ToggleDevice: ({ commit }, device) => {
      commit('TOGGLE_DEVICE', device)
    },
    SetAppVersion: async ({ commit }) => {
      const appRes = await getAppVersion()
      console.log(appRes, 'getAppversion')

      if (appRes.status === 200) {
        commit('SET_APP_VERSION', appRes.data.url)
      }
    }
  }
}

export default app
